package com.lihuinch.future;

import com.lihuinch.MqttTest;
import org.fusesource.mqtt.client.FutureConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;

import java.util.Properties;

/**
 * @author lihuinch
 * @date 2019/1/31 9:02
 */
public class MqttPublishAlways {
    private final static String topicname = "test";


    public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        prop.load(MqttTest.class.getResourceAsStream("/apollo.properties"));
        String user = prop.getProperty("apollo.user");
        String password = prop.getProperty("apollo.password");
        String host = prop.getProperty("apollo.host");
        Integer port = Integer.parseInt(prop.getProperty("apollo.port"));

        MQTT mqtt = new MQTT();
        mqtt.setHost(host, port);
        mqtt.setUserName(user);
        mqtt.setPassword(password);


        final FutureConnection connection = mqtt.futureConnection();
        connection.connect().await();
        int count = 0;
        while (true) {
            count++;
            // 用于发布消息，目前手机段不需要向服务端发送消息
            //主题的内容
            String message = "Hello " + count + " MQTT...";


            connection.publish(topicname, message.getBytes(), QoS.EXACTLY_ONCE,
                    false);
            System.out.println("MQTTFutureServer.publish Message " + "Topic Title :" + topicname + " context :" + message);

            Thread.sleep(2000);
        }

    }

}
